var topBar = null;
var putBar = null;

document.ready(function() {
	setValue('menuid', getUrlParam('menuid'));

    topBar = getHtml('writeBar1');
    putBar = getHtml('writeBar2');
    setHtml('writeBar1', '');
    setHtml('writeBar2', '');

	queryKey('ok', 'yes');
    getSortationList();
});

function getSortationList() {
    var param = {};
    param.ctrl = 'other';
    param.func = 'getSortationList';

    sendAjax(param, function(result) {
        setSelectItem('selectList2', result.sortationList);
    });
}

function queryKey(flag, permission = 'no') {
    openLoad();
    resetPageBar(flag);

    var param = {};
    param.ctrl = 'other';
    param.func = 'getOtherList';
    param.status = getAjaxValue('selectList1');
    param.sortation = getAjaxValue('selectList2');
    param.istop = getAjaxValue('selectList3');
    param.keyword = getAjaxValue('keyword1');
    param.page = getAjaxValue('page');
    param.menuid = getAjaxValue('menuid');
    param.permission = permission;

    sendAjax(param, function(result) {
        other.list = result.otherList;

        if (result.permission != null && result.permission != '') {
            other.permission = result.permission;
            setDisplay('addInfoBtn', other.permission.addinfo == 'yes' ? '' : 'none');
            setDisplay('resetBtn', other.permission.sequence == 'yes' ? '' : 'none');
            setDisplay('sortationBtn', other.permission.sortation == 'yes' ? '' : 'none');
        }

        loadPageBar(getValue('page'), result.allPages, result.rowCount);
        setHtml('listCount', result.listCount);

        setDisplay('mainBg', '');
        closeLoad();
    });
}

function setDateSelector(id) {
    layui.use('laydate', function(){
        var laydate = layui.laydate;
        laydate.render({
            elem: '#' + id
        });
    });
}

function addOther() {
    openSlipIframeDiv('admin.php?m=other&c=add');
}

function closeWriteDiv() {
    closeFloat();
}

function resetSequence() {
    var param = {};
    param.ctrl = 'other';
    param.func = 'resetSequence';

    sendAjax(param, function(result) {
        showTip('重新排序成功！');
    });
}

function toSortation() {
    openSlipIframeDiv('admin.php?m=other&c=sortation');
}

function putOther(id) {
    setValue('tempid', id);
    openConfirm('确定发布吗？', tipleft, function() {
        var param = {};
        param.ctrl = 'other';
        param.func = 'putOther';
        param.otherid = getAjaxValue('tempid');

        sendAjax(param, function(result) {
            submitRecord('edit', '其他服务列表页', '发布', getValue('tempid'));
            queryKey('page');
        });
    });
}

function retrieveOther(id) {
    setValue('tempid', id);
    openConfirm('确定回收吗？', tipleft, function() {
        var param = {};
        param.ctrl = 'other';
        param.func = 'retrieveOther';
        param.otherid = getAjaxValue('tempid');

        sendAjax(param, function(result) {
            submitRecord('edit', '其他服务列表页', '回收', getValue('tempid'));
            queryKey('page');
        });
    });
}

function pullOther(id) {
    setValue('tempid', id);
    openConfirm('确定下架吗？', tipleft, function() {
        var param = {};
        param.ctrl = 'other';
        param.func = 'pullOther';
        param.otherid = getAjaxValue('tempid');

        sendAjax(param, function(result) {
            submitRecord('edit', '其他服务列表页', '下架', getValue('tempid'));
            queryKey('page');
        });
    });
}

function restoreOther(id) {
    setValue('tempid', id);
    openConfirm('确定恢复吗？', tipleft, function() {
        var param = {};
        param.ctrl = 'other';
        param.func = 'restoreOther';
        param.otherid = getAjaxValue('tempid');

        sendAjax(param, function(result) {
            submitRecord('edit', '其他服务列表页', '恢复', getValue('tempid'));
            queryKey('page');
        });
    });
}

function deleteOther(id) {
    setValue('tempid', id);
    openConfirm('确定删除吗？', tipleft, function() {
        var param = {};
        param.ctrl = 'other';
        param.func = 'deleteOther';
        param.otherid = getAjaxValue('tempid');

        sendAjax(param, function(result) {
            submitRecord('edit', '其他服务列表页', '删除', getValue('tempid'));
            queryKey('page');
        });
    });
}

function putOtherAgain(id) {
    setValue('tempid', id);
    setValue('flag', 'add');
    openFloat('600px', 'auto', '70px', lyFloatLeft, putBar);
    setHtml('writeTitle', '重新发布');
    setDateSelector('expiretime');
}

function topOther(id, expiretime) {
    setValue('tempid', id);
    setValue('flag', 'add');
    openFloat('600px', 'auto', '70px', lyFloatLeft, topBar);
    setHtml('writeTitle', '置顶');
    setValue('expiretime', expiretime);
    setHtml('expiretimeTip', '【展示到期时间：' + expiretime + '】');
    setDateSelector('topendtime');
}

function setTopEndtime(days) {
    setValue('topendtime', get_add_day(days));

    if (getValue('topendtime') + get_now_time() > getValue('expiretime')) {
        showTip('置顶时长不能超过展示时间');
        setValue('topendtime', '');
        return;
    }
}

function setExpiretime(days) {
    setValue('expiretime', get_add_day(days));
}

function saveTopOther() {
    if (!getItemByName('toptype').checked && !getItemByName('toptype', 1).checked) {
        showTip('请选择置顶类型');
        return;
    }
    if (isEmpty('topendtime')) {
        showTip('请选择结束时间');
        return;
    }
    if (getValue('topendtime') + get_now_time() > getValue('expiretime')) {
        showTip('置顶时长不能超过展示时间');
        setValue('topendtime', '');
        return;
    }

    var param = {};
    param.ctrl = 'other';
    param.func = 'topOther';
    param.otherid = getAjaxValue('tempid');
    param.topendtime = getAjaxValue('topendtime');
    param.toptype = getItemByName('toptype').checked ? '1' : '2';

    sendAjax(param, function(result) {
        submitRecord('edit', '其他服务列表页', '置顶', getValue('tempid'));
        closeFloat();
        queryKey('page');
    });
}

function saveAgainOther() {
    if (isEmpty('expiretime')) {
        showTip('请填写到期时间');
        return;
    }

    var param = {};
    param.ctrl = 'other';
    param.func = 'putOtherAgain';
    param.otherid = getAjaxValue('tempid');
    param.expiretime = getAjaxValue('expiretime');
    param.status = getItemByName('status').checked ? '0' : '1';

    sendAjax(param, function(result) {
        submitRecord('edit', '其他服务列表页', '重新发布', getValue('tempid'));
        closeFloat();
        queryKey('page');
    });
}

function viewOther(otherid) {
    openSlipIframeDiv('admin.php?m=other&c=info&otherid=' + otherid + '&menuid=' + getValue('menuid'));
}

function pushToTweet(id, title, sortation) {
    var param = {};
    param.ctrl = 'tweet';
    param.func = 'pushToTweet';
    param.infoid = id;
    param.title = encodeURIComponent(title);
    param.sortation = encodeURIComponent(sortation);

    sendAjax(param, function(result) {
        showTip('文章已推选至公众号管理员审核');
    });
}

function pushToExclusive(id, sortation) {
    var param = {};
    param.ctrl = 'recommend';
    param.func = 'pushToExclusive';
    param.infoid = id;
    param.tabtype = '0';
    param.sortation = encodeURIComponent(sortation);
    param.datatype = 'other';

    sendAjax(param, function(result) {
        if (result.flag == 'add') {
            showTip('信息已推荐至首页');
        }
        else if (result.flag == 'edit') {
            showTip('推荐信息已更新成功！');
        }
        else {
            showTip('ERROR：推荐异常');
        }
    });
}

function clickOperateItem(id, title, sortation, event) {
    var flag = event.currentTarget.options[event.currentTarget.selectedIndex].value;
    if (flag == 'edit') {
        openSlipIframeDiv('admin.php?m=other&c=edit&otherid=' + id);
    }
    else if (flag == 'log') {
        openSlipIframeDiv('admin.php?m=other&c=log&association=' + id + '&title=' + escape('其他服务管理'));
    }
    else if (flag == 'company') {
        openSlipIframeDiv('admin.php?m=other&c=verify&type=company&infoid=' + id);
    }
    else if (flag == 'tweet') {
        pushToTweet(id, title, sortation);
    }
    else if (flag == 'exclusive') {
        pushToExclusive(id, sortation);
    }
    else {
        showTip('菜单类型异常');
    }
    
    event.currentTarget.options[0].selected = true;
}